package cn.tedu.jewelry.controller;

import cn.tedu.jewelry.dto.ProductDTO;
import cn.tedu.jewelry.entity.Admin;
import cn.tedu.jewelry.entity.Product;
import cn.tedu.jewelry.mapper.*;
import cn.tedu.jewelry.restful.JsonResult;
import cn.tedu.jewelry.vo.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

//后台管理员控制器
@Slf4j
@Api(tags = "1-后台管理")
@RestController
@RequestMapping("/admin")
public class AdminController {

    //管理员mapper
    @Autowired
    private AdminMapper adminMapper;

    //用户mapper
    @Autowired
    private UserMapper userMapper;

    //订单mapper
    @Autowired
    private OrderMapper orderMapper;

    //品牌mapper
    @Autowired
    private BrandMapper brandMapper;

    //分类mapper
    @Autowired
    private CategoryMapper categoryMapper;

    //商品mapper
    @Autowired
    private ProductMapper productMapper;

    //轮播图mapper
    @Autowired
    private BannerMapper bannerMapper;

    //上传图片的控制器类
    @Autowired
    private UploadController uploadController;

    //管理员登录
    @ApiOperationSupport(order = 10)
    @ApiOperation("后台登录")//在左边列表显示名称
    @PostMapping("/login")
    public JsonResult<AdminStandVO> adminLogin(Admin admin,HttpSession session){

        //通过获取到的管理员用户名，查询管理员表里的数据
        AdminStandVO result = adminMapper.adminLogin(admin.getUsername());

        //查询到有用户名时，作比较
        if (result != null){
            if (result.getPassword().equals(admin.getPassword())){
                result.setPassword("***");
                log.debug("登录成功，管理员信息是：{}",result);
                //session.setAttribute("admin",result);
                return JsonResult.ok(result);
            }
        }
        return JsonResult.ok(new AdminStandVO());//用户名或密码错误
    }
    /****************************LIST 1XX***********************************/

    //用户列表
    @ApiOperationSupport(order = 101)
    @ApiOperation("用户列表")//在左边列表显示名称
    @GetMapping("/user/list")
    public JsonResult<List<UserStandVO>> getUserList(){
        return JsonResult.ok(userMapper.getUserList());
    }

    //分类列表
    @ApiOperationSupport(order = 102)
    @ApiOperation("分类列表")//在左边列表显示名称
    @GetMapping("/category/list")
    public JsonResult<List<CategoryStandVO>> getCategoryList() {
        return JsonResult.ok(categoryMapper.getCategoryList());
    }

    //品牌列表
    @ApiOperationSupport(order = 103)
    @ApiOperation("品牌列表")//在左边列表显示名称
    @GetMapping("/brand/list")
    public JsonResult<List<BrandStandVO>> getBrandList(){
        return JsonResult.ok(brandMapper.getBrandList());
    }

    //商品列表
    @ApiOperationSupport(order = 104)
    @ApiOperation("商品列表")//在左边列表显示名称
    @GetMapping("/product/list")
    public JsonResult<List<ProductBrandCategoryVO>> getProductList() {
        //获取商品+品牌信息+分类信息
        List<ProductBrandCategoryVO> list = productMapper.getProductAndBrandAndCategory();
        return JsonResult.ok(list);
    }

    //轮播图列表
    @ApiOperationSupport(order = 105)
    @ApiOperation("轮播图列表")//在左边列表显示名称
    @GetMapping("/banner/list")
    public JsonResult<List<BannerStandVO>> bannerList(){
        return JsonResult.ok(bannerMapper.getBannerList());
    }

    /****************************SELECT 2XX***********************************/

    //用户查询:根据用户名查询
    @ApiOperationSupport(order = 201)
    @ApiOperation("用户查询")//在左边列表显示名称
    @GetMapping("/user/by/{username}")
    public JsonResult<UserStandVO> getUserByUserName(@PathVariable String username){
        return JsonResult.ok(userMapper.getUserByUserName(username));
    }

    //用户地址管理:获取所有用户地址
    @ApiOperationSupport(order = 202)
    @ApiOperation("所有用户地址列表")//在左边列表显示名称
    @GetMapping("/user/address/list")
    public JsonResult<List<UserAddressStandVO>> getUserAddressList(){
        return JsonResult.ok(userMapper.getUserAddressList());
    }

    //用户订单管理:获取所有订单
    @ApiOperationSupport(order = 203)
    @ApiOperation("所有用户订单列表")//在左边列表显示名称
    @GetMapping("/user/order/list")
    public JsonResult<List<OrderStandVO>> getUserOrderList(){
        return JsonResult.ok(orderMapper.getUserOrderList());
    }

    //用户订单查询:按手机号查询
    @ApiOperationSupport(order = 204)
    @ApiOperation("按手机号码查询订单")//在左边列表显示名称
    @GetMapping("/user/order/by/{phone:[0-9]+}")
    public JsonResult<OrderStandVO> getUserOrderByPhone(@PathVariable Long phone){
        return JsonResult.ok(orderMapper.getUserOrderByPhone(phone));
    }

    //用户订单查询:按联系人查询
    @ApiOperationSupport(order = 205)
    @ApiOperation("按联系人查询订单")//在左边列表显示名称
    @GetMapping("/user/order/by/contactname/{contactName}")
    public JsonResult<OrderStandVO> getOrderByUserName(@PathVariable String contactName){
        return JsonResult.ok(orderMapper.getOrderByUserName(contactName));
    }

    //用户订单查询:按订单号查询订单
    @ApiOperationSupport(order = 206)
    @ApiOperation("按订单号查询订单")//在左边列表显示名称
    @GetMapping("/user/order/by/ordernumber/{orderNumber}")
    public JsonResult<OrderStandVO> getOrderByOrderNumber(@PathVariable String orderNumber){
        return JsonResult.ok(orderMapper.getOrderByOrderNumber(orderNumber));
    }

    //用户地址:按用户名查询地址
    @ApiOperationSupport(order = 207)
    @ApiOperation("按用户名查询地址")//在左边列表显示名称
    @GetMapping("/user/address/by/contactname/{contactName}")
    public JsonResult<List<UserAddressStandVO>> getUserAddressByContactName(@PathVariable String contactName){
        return JsonResult.ok(userMapper.getUserAddressByContactName(contactName));
    }

    //用户地址:通过id获取用户地址信息
    @ApiOperationSupport(order = 208)
    @ApiOperation("按用户名查询地址")//在左边列表显示名称
    @GetMapping("/user/address/by/userid/{userId}")
    public JsonResult<List<UserAddressStandVO>> getUserAddressByUserId(@PathVariable Long userId){
        return JsonResult.ok(userMapper.getUserAddressByUserId(userId));
    }

    //通过品牌id获取品牌信息
    @ApiOperationSupport(order = 209)
    @ApiOperation("id获取品牌")//在左边列表显示名称
    @GetMapping("/brand/{id:[1-9]+}")
    public JsonResult<BrandStandVO> getBrandById(@PathVariable Long id) {
        return JsonResult.ok(brandMapper.getBrandById(id));
    }

    //通过品牌id获取品牌信息
    @ApiOperationSupport(order = 210)
    @ApiOperation("id获取分类")//在左边列表显示名称
    @GetMapping("/category/{id:[1-9]+}")
    public JsonResult<CategoryStandVO> getCategoryById(@PathVariable Long id) {
        return JsonResult.ok(categoryMapper.getCategoryById(id));
    }



    /****************************INSERT 3XX***********************************/

    //添加品牌
    @ApiOperationSupport(order = 301)
    @ApiOperation("添加品牌")//在左边列表显示名称
    @GetMapping("/add/brand/{brandName}")
    public JsonResult<Void> addBrand(@PathVariable String brandName){
        brandMapper.insert(brandName);
        return JsonResult.ok();
    }

    //添加分类
    @ApiOperationSupport(order = 302)
    @ApiOperation("添加分类")//在左边列表显示名称
    @GetMapping("/add/category/{categoryName}")
    public JsonResult<Void> addCategory(@PathVariable String categoryName){
        categoryMapper.insert(categoryName);
        return JsonResult.ok();
    }

    //添加轮播图
    @ApiOperationSupport(order = 303)
    @ApiOperation("添加分类")//在左边列表显示名称
    @GetMapping("/add/banner/{bannerName}/url/{url}")
    public JsonResult<Void> addBanner(@PathVariable String bannerName,@PathVariable String url){
        bannerMapper.addBanner(bannerName,"/" + url);
        return JsonResult.ok();
    }

    //添加商品
    @ApiOperationSupport(order = 304)
    @ApiOperation("添加商品")//在左边列表显示名称
    @PostMapping("/add/product")
    public JsonResult<Void> addProduct(@RequestBody ProductDTO productDTO){
        productDTO.setCode(productDTO.getBrandId() + "" + productDTO.getBrandId());
        productDTO.setViewCount(0L);
        productDTO.setSaleCount(0L);
        productDTO.setStock(0L);
        productDTO.setGmtCreated(LocalDateTime.now());
        productDTO.setEnable(0);
        productMapper.insert(productDTO);
        return JsonResult.ok();
    }

    /****************************改 5XX***********************************/

    //修改品牌启用状态
    @ApiOperationSupport(order = 501)
    @ApiOperation("修改品牌启用状态")//在左边列表显示名称
    @GetMapping("/update/brand/{id}/enable/{enable}")
    public JsonResult<Void> updateBrandByIdAndEnable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
        brandMapper.updateBrandByIdAndEnable(id,enable);
        return JsonResult.ok();
    }

    //修改分类启用状态
    @ApiOperationSupport(order = 502)
    @ApiOperation("修改轮播图状态")//在左边列表显示名称
    @GetMapping("/update/category/{id}/enable/{enable}")
    public JsonResult<Void> updateCategoryByIdAndEnable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
        categoryMapper.updateCategoryByIdAndEnable(id,enable);
        return JsonResult.ok();
    }

    //修改轮播图状态
    @ApiOperationSupport(order = 503)
    @ApiOperation("修改轮播图状态")//在左边列表显示名称
    @GetMapping("/update/banner/{id}/enable/{enable}")
    public JsonResult<BannerStandVO> updateBannerByIdAndEnable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
        bannerMapper.updateBannerByIdAndEnable(id,enable);
        BannerStandVO banner = bannerMapper.getBannerById(id);//通过id获取轮播图
        return JsonResult.ok(banner);
    }

    //修改商品启用状态
    @ApiOperationSupport(order = 504)
    @ApiOperation("修改商品启用状态")//在左边列表显示名称
    @GetMapping("/update/product/{id}/enable/{enable}")
    public JsonResult<Void> updateProductByIdAndEnable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
        productMapper.updateProductByIdAndEnable(id,enable);
        return JsonResult.ok();
    }

    //修改用户启用状态
    @ApiOperationSupport(order = 505)
    @ApiOperation("修改用户启用状态")//在左边列表显示名称
    @GetMapping("/update/user/{id}/enable/{enable}")
    public JsonResult<Void> updateUserByIdAndEnable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
        userMapper.updateUserByIdAndEnable(id,enable);
        return JsonResult.ok();
    }

    /****************************DELETE 6XX***********************************/

    //id删除品牌
    @ApiOperationSupport(order = 601)
    @ApiOperation("id删除分类")//在左边列表显示名称
    @GetMapping("/delete/brand/{id}")
    public JsonResult<Void> deleteBrandById(@PathVariable Long id){
        brandMapper.deleteBrandById(id);
        return JsonResult.ok();
    }

    //id删除分类
    @ApiOperationSupport(order = 602)
    @ApiOperation("id删除分类")//在左边列表显示名称
    @GetMapping("/delete/category/{id}")
    public JsonResult<Void> deleteCategoryById(@PathVariable Long id){
        categoryMapper.deleteCategoryById(id);
        return JsonResult.ok();
    }

    //id删除轮播图
    @ApiOperationSupport(order = 603)
    @ApiOperation("id删除轮播图")//在左边列表显示名称
    @GetMapping("/delete/banner/{id}")
    public JsonResult<Void> deleteBannerById(@PathVariable Long id){
        BannerStandVO banner = bannerMapper.getBannerById(id);
        uploadController.remove(banner.getUrl());
        bannerMapper.deleteBannerById(id);
        return JsonResult.ok();
    }

    //id删除商品
    @ApiOperationSupport(order = 604)
    @ApiOperation("id删除商品")//在左边列表显示名称
    @GetMapping("/delete/product/{id}")
    public JsonResult<Void> deleteProductById(@PathVariable Long id){
        ProductStandVO product = productMapper.getProductById(id);
        if(product.getUrl() != null){
            uploadController.remove(product.getUrl());
        }
        productMapper.deleteById(id);
        return JsonResult.ok();
    }
}